<?php

class APIDefinition_Object_Controller {

  protected $_name;
  protected $_moduleName;
  protected $_description;
  protected $_creatorEmail;
  protected $_creationDate;
  
  /* definition of the nonce table */
  static protected $_dbControllerTableName = 'controller';
  static protected $_dbControllerNameCol = 'name';
  static protected $_dbControllerModuleNameCol = 'moduleName';
  static protected $_dbControllerDescriptionCol = 'description';
  static protected $_dbControllerCreatorEmailCol = 'creatorEmail';
  static protected $_dbControllerCreationdateCol = 'creationDate';  

  public function __construct($name,$moduleName,$data = array()) {
  	
  	if(empty($data)) {
   	  //We validate the name
  	  self::_validateName($name);
  	  self::_validateName($moduleName);
  	  //We get the data from the database
      $db = Zend_Registry::get('dt_dbs')->getDb('apidefinition','controller',$moduleName);
      $select = $db->select()->from(self::$_dbControllerTableName);
      $select->where($db->quoteInto(self::$_dbControllerNameCol ." = ?", $name));
      $select->where($db->quoteInto(self::$_dbControllerModuleNameCol ." = ?", $moduleName));
      $res = $db->fetchAll($select->__toString());
    
      if(empty($res)) {
  	    require_once('apidefinition/objects/Exception.php');
  	    throw new APIDefinition_Object_Exception('Controller not found',6); 
      }
    $data = $res[0];
  	}
    $this->_name = $data[self::$_dbControllerNameCol];
    $this->_moduleName = $data[self::$_dbControllerModuleNameCol];
    $this->_description = $data[self::$_dbControllerDescriptionCol];
    $this->_creatorEmail = $data[self::$_dbControllerCreatorEmailCol];
    $this->_creationDate = $data[self::$_dbControllerCreationdateCol];	

      	
  }	//end constructor

  public function asArray() {
  	$results = array();
    $results['name'] = $this->_name;
    $results['moduleName'] = $this->_moduleName;
    $results['description'] = $this->_description;
    $results['creatorEmail'] = $this->_creatorEmail;
    $results['creationDate'] = $this->_creationDate;	  	
    return $results;
  }
	
  static public function listing($moduleName) {
    $db = Zend_Registry::get('dt_dbs')->getDb('apidefinition','controller',$moduleName);
    $select = $db->select()->from(self::$_dbControllerTableName);
    $select->where($db->quoteInto(self::$_dbControllerModuleNameCol ." = ?", $moduleName));
    $res = $db->fetchAll($select->__toString());  

    if(empty($res)) return array();

    //We must reinterpret the rows so it is independant to the way it is stored
    $results = array();
    foreach($res as $num => $row) {
    	$controller = new APIDefinition_Object_Controller('','',$row);
    	$results[] = $controller->asArray();	
    }
    
    return $results;
      
  } //end list
  
  
  
  static public function create($params) {
  	//check if mandatory parameters are provided
  	if(empty($params['description'])) {
  	  require_once('apidefinition/objects/Exception.php');
  	  throw new APIDefinition_Object_Exception('Mandatory data is missing',1);
  	} //end empty parameters
  	
  	//We clean the data provided
  	//We validate the name
  	self::_validateName($params['name']);
  	$name = $params['name'];
  	
    require_once('apidefinition/objects/module.php');
    $module = new APIDefinition_Object_Module($params['moduleName']);
  	$moduleName = $module->getName();
  	
  	//We clean the description, only constraint is that it should be valid HTML
	require_once('htmlpurifier/library/HTMLPurifier.auto.php');
  	$config = HTMLPurifier_Config::createDefault();
  	$config->set('HTML.Allowed', '');
  	$purifier = new HTMLPurifier($config);
    $description = $purifier->purify($params['description']);

    if(empty($description)) {
  	  require_once('apidefinition/objects/Exception.php');
  	  throw new APIDefinition_Object_Exception('Controller definition is empty',4);    	
    }
    
    //We check if the Controller is not already existing
    $existing = true;
    try{
    	$controller = new APIDefinition_Object_Controller($name,$moduleName);
    } catch(APIDefinition_Object_Exception $e) {
    	$existing = false;	
    }
    if($existing) {
  	    require_once('apidefinition/objects/Exception.php');
  	    throw new APIDefinition_Object_Exception('Controller is already existing',7);     	
    }


    //OK we can create the Controller
	$email = Zend_Registry::get('app_user')->getEmail();
  	$creationDate = time();
  	
  	$data = array(
      self::$_dbControllerNameCol => $name,
      self::$_dbControllerModuleNameCol => $moduleName,
      self::$_dbControllerDescriptionCol => $description,
      self::$_dbControllerCreatorEmailCol => $email,
      self::$_dbControllerCreationdateCol => $creationDate  	
  	);
    $db = Zend_Registry::get('dt_dbs')->getDb('apidefinition','controller',$moduleName);
    $db->insert(self::$_dbControllerTableName, $data); 	
  	return true;
  	
  } //end create

  public function getName() {
    return $this->_name;	
  }

  static protected function	_validateName($name) {
    
    if(empty($name)) {
  	  require_once('apidefinition/objects/Exception.php');
  	  throw new APIDefinition_Object_Exception('Mandatory data is missing',1);
    }
    
    //Controller and module name can only be lowercase alphabet characters and less or equal 32 characters
  	if(preg_match('/[^a-z]/',$name)) {
  	  require_once('apidefinition/objects/Exception.php');
  	  throw new APIDefinition_Object_Exception('Controller or module name can only be alphabet lower case characters',2);  		
  	}
  	if(32 < strlen($name)) {
  	  require_once('apidefinition/objects/Exception.php');
  	  throw new APIDefinition_Object_Exception('Controller or module name must be less than 33 characters',3);
  	}
  	return true;	
  }
}